<?php

namespace App\Ad\Postback\Handle;

use App\Ad\Docking\DockingBase;
use App\Commons\CommonConstant;
use App\Models\AdMonitor;
use App\Models\FirstupAd;
use App\Models\Log\LogAdPostbackMedia;
use App\Models\Users;

// 注册回传
class RegPostback extends PostbackBase
{
    public function handle()
    {
        $log = $this->log;
        $user = Users::find($log->mod_id);
        $firstup_ad = FirstupAd::find($log->firstup_ad_id);
        $monitor    = AdMonitor::find($firstup_ad->monitor_id);
        if (!$monitor) {
            $monitor = new AdMonitor;
        }

        $docking = DockingBase::getClass($log->media_docking_id);
        try {
            $status = $docking->postbackToMedia(CommonConstant::EVENT_REG, $monitor, $firstup_ad, $user);
        } catch (\Throwable $th) {
            $status = LogAdPostbackMedia::STATUS_ERROR;
        }

        $log->check_num++;
        $log->check_time = time();
        $log->status = $status;
        $log->status_time = time();
        $log->save();

        return true;
    }
}
